package exception

import (
	"fmt"

	"github.com/pkg/errors"
)

type BusinessError interface {
	error

	Model() string

	Code() int

	Message() string

	Unwrap() error
}

// BusinessException
// Title    		BusinessException
// Description   	业务异常实现类
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Update  			alittlexincan@163.com         	2024/07/22 11:36
type BusinessException struct {

	// model 业务异常模块名称
	model string

	// code 业务异常状态码
	code int

	// message 业务异常状态信息
	message string

	// wrap 底层错误, 用于构建错误链
	wrap error
}

func NewBusinessException(code int, message string) *BusinessException {
	return &BusinessException{
		code:    code,
		message: message,
	}
}

// BusinessExceptionBuilder
// Title 			BusinessExceptionBuilder
// Description 		创建业务异常实例
// Author      		alittlexincan@163.com       							2024/07/22 11:36
// Update  			alittlexincan@163.com       							2024/07/22 11:36
// Return    		*BusinessException										返回业务异常实例
func BusinessExceptionBuilder() *BusinessException {
	return &BusinessException{}
}

// WithModel
// Title 			WithModel
// Description 		构造器: 设置业务异常模块名称
// Author      		alittlexincan@163.com       							2024/07/22 11:36
// Update  			alittlexincan@163.com       							2024/07/22 11:36
// Param    		model 						string						异常模块名称
// Return    		*BusinessException										返回业务异常实例
func (exception *BusinessException) WithModel(model string) *BusinessException {
	exception.model = model
	return exception
}

// WithCode
// Title 			WithCode
// Description 		构造器: 设置业务异常码
// Author      		alittlexincan@163.com       							2024/07/22 11:36
// Update  			alittlexincan@163.com       							2024/07/22 11:36
// Param    		code 						int							异常状态码
// Return    		*BusinessException										返回业务异常实例
func (exception *BusinessException) WithCode(code int) *BusinessException {
	exception.code = code
	return exception
}

// WithMessage
// Title 			WithMessage
// Description 		构造器: 设置业务错误信息
// Author      		alittlexincan@163.com       							2024/07/22 11:36
// Update  			alittlexincan@163.com       							2024/07/22 11:36
// Param    		message 					string						异常信息
// Return    		*BusinessException										返回业务异常实例
func (exception *BusinessException) WithMessage(message string) *BusinessException {
	exception.message = message
	return exception
}

// WithError
// Title 			WithError
// Description 		构造器: 设置错误链
// Author      		alittlexincan@163.com       							2024/07/22 11:36
// Update  			alittlexincan@163.com       							2024/07/22 11:36
// Param    		err 						error						错误链
// Return    		*BusinessException										返回业务异常实例
func (exception *BusinessException) WithError(err error) *BusinessException {
	exception.wrap = errors.WithStack(err)
	return exception
}

// Build
// Title 			Build
// Description 		创建业务异常实例
// Author      		alittlexincan@163.com       							2024/07/22 11:36
// Update  			alittlexincan@163.com       							2024/07/22 11:36
// Return    		*BusinessException										返回业务异常实例
func (exception *BusinessException) Build() *BusinessException {
	return exception
}

// Error
// Title    		Error
// Description   	实现error接口, 重新写语言内置的error的Error方法
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Update  			alittlexincan@163.com         	2024/07/22 11:36
// Return    		string							返回异常信息
func (exception *BusinessException) Error() string {
	if exception.wrap != nil {
		return fmt.Sprintf("%s: %s, %v", exception.model, exception.message, exception.wrap)
	}
	return exception.message
}

// Unwrap
// Title    		Unwrap
// Description   	获取错误链
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Update  			alittlexincan@163.com         	2024/07/22 11:36
// Return    		error							返回错误链
func (exception *BusinessException) Unwrap() error { return exception.wrap }

// Model
// Title    		Model
// Description   	获取异常模块名称
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Update  			alittlexincan@163.com         	2024/07/22 11:36
// Return    		string							返回异常模块名称
func (exception *BusinessException) Model() string { return exception.model }

// Code
// Title    		Code
// Description   	获取异常状态码
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Update  			alittlexincan@163.com         	2024/07/22 11:36
// Return    		int								返回异常状态码
func (exception *BusinessException) Code() int { return exception.code }

// Message
// Title    		Message
// Description      获取异常信息
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Update  			alittlexincan@163.com         	2024/07/22 11:36
// Return    		string							返回异常信息
func (exception *BusinessException) Message() string { return exception.message }
